package com.the4man.framework.interceptor;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
import com.the4man.framework.utils.RequestUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.util.HashSet;

/**
 * Created by lenovo on 2017/12/12.
 */
public class SessionInterceptor extends MethodFilterInterceptor {
    private static final Logger logger = LoggerFactory.getLogger(SessionInterceptor.class);
    private static HashSet<String> noNeedLoginUrl = new HashSet();

    static {
        noNeedLoginUrl.add("/cnbdw/channelInfoAction!getAllChannelInfo.action");
        noNeedLoginUrl.add("/cnbdw/newsInfoAction!getNewsInfosByChannelId.action");
        noNeedLoginUrl.add("/cnbdw/newsInfoAction!getNewsInfoDetail.action");
        noNeedLoginUrl.add("/cnbdw/systemManagerAction!addUserInfo.action");
        noNeedLoginUrl.add("/cnbdw/userAction!clientlogin.action");
        noNeedLoginUrl.add("/cnbdw/newsInfoAction!getNewsInfoDetail.action");
        noNeedLoginUrl.add("/cnbdw/userAction!clientlogout.action");
        noNeedLoginUrl.add("/cnbdw/investInfoAction!getInvestInfos.action");
        noNeedLoginUrl.add("/cnbdw/investInfoAction!getInfoDetail.action");
        noNeedLoginUrl.add("/cnbdw/sellInfoAction!getSellInfos.action");
        noNeedLoginUrl.add("/cnbdw/sellInfoAction!getInfoDetail.action");
        noNeedLoginUrl.add("/cnbdw/buyInfoAction!getBuyInfos.action");
        noNeedLoginUrl.add("/cnbdw/buyInfoAction!getInfoDetail.action");
        noNeedLoginUrl.add("/cnbdw/expertsForumAction!getExpertsForumInfos.action");
        noNeedLoginUrl.add("/cnbdw/expertsForumAction!getExpertsForumDetail.action");
        noNeedLoginUrl.add("/cnbdw/recomProductAction!getRecomProductInfos.action");
        noNeedLoginUrl.add("/cnbdw/recomProductAction!getDetailInfos.action");
        noNeedLoginUrl.add("/cnbdw/recomExhibitionAction!getRecomExhibitionInfos.action");
        noNeedLoginUrl.add("/cnbdw/recomExhibitionAction!getDetailInfos.action");
        noNeedLoginUrl.add("/cnbdw/companyInfoAction!getCompanyInfoInfos.action");
        noNeedLoginUrl.add("/cnbdw/companyInfoAction!getCompanyInfoDetail.action");
        noNeedLoginUrl.add("/cnbdw/expertsForumAction!getExpertsForumDetail.action");
        noNeedLoginUrl.add("/cnbdw/hallFrameAction!getHallFrameInfos.action");
        noNeedLoginUrl.add("/cnbdw/hallFrameAction!getHallFrameDetail.action");
        noNeedLoginUrl.add("/cnbdw/tradeAssociationsAction!getTradeAssociationsInfos.action");
        noNeedLoginUrl.add("/cnbdw/tradeAssociationsAction!getTradeAssociationsDetail.action");
        noNeedLoginUrl.add("/cnbdw/advertiseAction!getInfoByAdPositionId.action");
        noNeedLoginUrl.add("/cnbdw/focusNewsAction!getNewsInfosByChannelId.action");
        noNeedLoginUrl.add("/cnbdw/pushMsgInfoAction!getNewsInfosByChannelId.action");
        noNeedLoginUrl.add("/cnbdw/newsInfoAction!search.action");
        noNeedLoginUrl.add("/cnbdw/channelEntityAction!getChannelEntityList.action");
        noNeedLoginUrl.add("/cnbdw/programEntityAction!getProgramList.action");
        noNeedLoginUrl.add("/cnbdw/appAgentAction!commit.action");
        noNeedLoginUrl.add("/cnbdw/commentsInfoAction!add.action");
        noNeedLoginUrl.add("/cnbdw/commentsInfoAction!getCommentsList.action");
        noNeedLoginUrl.add("/cnbdw/picNewsInfoAction!addFromApp.action");
        noNeedLoginUrl.add("/cnbdw/picNewsInfoAction!addFromApp.action");
        noNeedLoginUrl.add("/cnbdw/locAction!loc.action");
    }

    public SessionInterceptor() {
    }

    protected String doIntercept(ActionInvocation invocation) throws Exception {
        ActionContext actionContext = invocation.getInvocationContext();
        HttpServletRequest httpRequest = (HttpServletRequest)actionContext.get("com.opensymphony.xwork2.dispatcher.HttpServletRequest");
        String uri = this.parseUri(httpRequest);
        String suri = StringUtils.startsWith(uri, "/")?StringUtils.substringAfter(uri, "/"):uri;
        String cnbdwStart = StringUtils.startsWith(suri, "cnbdw")?suri:"cnbdw/" + suri;
        String cnbdwStart1 = "/" + cnbdwStart;
        String start = StringUtils.startsWith(suri, "cnbdw")?StringUtils.substringAfter(cnbdwStart, "cnbdw/"):suri;
        String start1 = "/" + start;
        if(!noNeedLoginUrl.contains(cnbdwStart) && !noNeedLoginUrl.contains(cnbdwStart1) && !noNeedLoginUrl.contains(start) && !noNeedLoginUrl.contains(start1)) {
            if(RequestUtil.getSessionUserinfo() == null) {
                ServletActionContext.getRequest().setAttribute("returnMsg", "您还没有登录或登录已超时，请重新登录！");
                return "missSession";
            } else {
                return invocation.invoke();
            }
        } else {
            return invocation.invoke();
        }
    }

    private String parseUri(HttpServletRequest request) {
        String contextPath = request.getContextPath();
        String uri = request.getRequestURI();
        if(!contextPath.equals("/")) {
            uri = uri.substring(contextPath.length());
        }

        return uri;
    }
}
